package com.spzx.product.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.api.domain.Brand;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.service.CategoryBrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/categoryBrand")

@Tag(name = "分类品牌管理模块")
public class CategoryBrandController extends BaseController {
    @Resource
    CategoryBrandService categoryBrandService;


    @Operation(summary = "新增商品：根据三级分类id加载分类下的品牌列表")
    @GetMapping("listBrandByC3Id/{c3id}")
    public AjaxResult save(@PathVariable("c3id") Long c3id){
     List<Brand > brands = categoryBrandService.selectBrandsByC3id(c3id);

        return success(brands);
    }


    @Operation(summary = "条件查询分类品牌分页数据")
    @GetMapping("list")
    public TableDataInfo list( CategoryBrand categoryBrand) {
        Page<CategoryBrand> page = categoryBrandService.listByCidAndBid(categoryBrand);
        return new TableDataInfo(page.getRecords(), (int) page.getTotal());
    }



    @Operation(summary = "查询分类品牌详情")
    @GetMapping("{id}")
    public AjaxResult getById(@PathVariable("id") Long id) {
        return success(categoryBrandService.getCategoryBrandById(id));
    }




    @Operation(summary = "新增分类品牌")
    @PostMapping
    public AjaxResult add(@Validated @RequestBody CategoryBrand categoryBrand) {
        if (!categoryBrandService.checkCategoryBrandUnique(categoryBrand)) {
            return error("新增品牌失败，品牌名称已存在");
        }
        categoryBrand.setCreateBy(SecurityUtils.getUsername());
        return toAjax(categoryBrandService.save(categoryBrand));
    }




    @Operation(summary = "更新分类品牌")
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody CategoryBrand categoryBrand) {

        if (!categoryBrandService.checkCategoryBrandUnique(categoryBrand)){
            return error(("更新分类品牌失败，分类品牌已存在"));

        }
        categoryBrand.setCreateBy(SecurityUtils.getUsername());
        return toAjax(categoryBrandService.updateById(categoryBrand));
    }




    @Operation(summary = "批量删除分类品牌")
    @DeleteMapping("{ids}")
    public AjaxResult remove(@PathVariable("ids") List <Long> ids){

        return toAjax(categoryBrandService.deleteById(ids));
    }

    @Operation(summary = "查询所有")
    @GetMapping
    public  AjaxResult listAll(){
        List<CategoryBrand> list = categoryBrandService.list();
        return success(list );
    }

}
